package com.aw.guiguzi.module.manager._user

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.aw.baselib.base.AwBaseEvent
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwRecyclerViewUtil
import com.aw.baselib.widgets.MySmartRefreshLayout
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.UserInfoBean
import com.aw.guiguzi.constants.MyConstant
import com.aw.guiguzi.databinding.ActivityManagerUserListBinding
import com.aw.guiguzi.event.EventRefreshUserListType
import com.aw.guiguzi.module.manager._user.adapter.UserListAdapter
import com.aw.guiguzi.module.manager._user.presenters.UserListPresent
import com.aw.guiguzi.module.manager._user.views.UserListView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter.RequestLoadMoreListener
import com.scwang.smartrefresh.layout.api.RefreshLayout

class UserListActivity : AwMvpBindingActivity<UserListPresent?, ActivityManagerUserListBinding?>(),
    UserListView.View, RequestLoadMoreListener {
    private var mAdapter: UserListAdapter? = null
    private var index = 1
    private var total = 0
    private var mList: MutableList<UserInfoBean> = ArrayList()
    override fun refreshByBus(type: AwBaseEvent) {
        super.refreshByBus(type)
        if (type is EventRefreshUserListType) {
            getData(true)
        }
    }

    override fun createPresenter(): UserListPresent {
        return UserListPresent(this)
    }

    override fun getViewBinding(): ActivityManagerUserListBinding {
        return ActivityManagerUserListBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbarWithBackWithRightImg("用户列表", R.drawable.icon_add_user) {
            toClass(
                UserCreateActivity::class.java, false
            )
        }
    }

    override fun initData() {
        super.initData()
        mAdapter = UserListAdapter()
        AwRecyclerViewUtil.setRecyclerViewLinearlayout(mActivity, binding?.rcvData, mAdapter, false)
        getData(true)
    }

    override fun initListener() {
        super.initListener()
        binding?.sflLayout!!.setOnRefreshListener { refreshLayout: RefreshLayout? -> getData(true) }
        mAdapter!!.setOnLoadMoreListener(this, binding?.rcvData)
        mAdapter!!.onItemClickListener =
            BaseQuickAdapter.OnItemClickListener { adapter: BaseQuickAdapter<*, *>, view: View?, position: Int ->
                val itemBean = adapter.getItem(position) as UserInfoBean?
                toClass(UserDetailActivity::class.java, false, Extras.params, itemBean)
            }
    }

    private fun getData(needResetIndex: Boolean) {
        if (needResetIndex) {
            index = 1
            mList = ArrayList()
            mAdapter!!.notifyDataSetChanged()
        }
        mPresenter!!.getUserList(index)
    }

    override fun onLoadMoreRequested() {
        if (mList.size < total) {
            index++
            getData(false)
        } else {
            if (index == 1 && mList.size < MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE) {
                mAdapter!!.loadMoreEnd(true)
            } else {
                mAdapter!!.loadMoreEnd()
            }
        }
    }

    override fun getUserListSuccess(total: Int, list: List<UserInfoBean>) {
        this.total = total
        binding?.sflLayout!!.finishRefresh()
        if (AwDataUtil.isEmpty(list)) {
            AwRecyclerViewUtil.resetView(mActivity, mAdapter, binding?.rcvData, index == 1)
            return
        }
        mList.addAll(list)
        mAdapter!!.addAllData(mList)
        mAdapter!!.loadMoreComplete()
        mAdapter!!.disableLoadMoreIfNotFullPage(binding?.rcvData)
    }

    override fun getUserListFail(code: Int, msg: String?) {
        binding?.sflLayout!!.finishRefresh()
        AwRecyclerViewUtil.resetView(mActivity, mAdapter, binding?.rcvData, index == 1)
    }
}